package servlet;

import model.Type;
import utils.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/typeList")
public class TypeListServlet extends HttpServlet {
   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
      resp.setContentType("application/json;charset=utf-8");

      List<Type> types = (List<Type>) req.getServletContext().getAttribute("typeList");

      // 转换为JSON字符串并输出
      String jsonString = JsonUtils.objectToString(types);
      System.out.println(jsonString);  // 控制台调试输出
      resp.getWriter().println(jsonString);
   }
}
